/*
 * author      : 吴中勤
 * date        : 2018/8/16
 * description : moby sdk的一些配置
 */

package com.deepexi.adapter.sdk.moby;

import com.deepexi.adapter.common.BaseDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class MobySdkConfig extends BaseDto {

    private static Logger logger = LoggerFactory.getLogger(MobySdkConfig.class.getName());
    private static final String PROPERTY_FILE_GLOBLE = "moby.properties";

    /**
     * 从配置文件中读取
     *
     * @param profile dev/prod/... 需要找到一个 sdk-{profile}
     * @return
     */
    public static MobySdkConfig fromResource(String profile) {
        logger.info("load SdkConfig: profile=" + profile);
        String profileConfig = "moby-" + profile + ".properties";
        Properties props = new Properties();
        try {
            // 全局
            InputStream inputStreamGlobal = MobySdkConfig.class
                    .getClassLoader()
                    .getResourceAsStream(PROPERTY_FILE_GLOBLE);
            props.load(inputStreamGlobal);

            // profile
            InputStream inputStreamProfile = MobySdkConfig.class
                    .getClassLoader()
                    .getResourceAsStream(profileConfig);
            props.load(inputStreamProfile);

        } catch (IOException ex) {
            ex.printStackTrace();
            throw new RuntimeException("load property file fail" + ex);
        }

        /** 从 property中读取一个配置 */
        MobySdkConfig sdkConfig = new MobySdkConfig();
        //
        sdkConfig.mobyHostCarCenter = props.getProperty("moby.host.car-center");
        sdkConfig.mobyHostProductCenter = props.getProperty("moby.host.product-center");

        sdkConfig.path4VehicleInfo = props.getProperty("path.vehicleInfo");
        sdkConfig.path4InteriorPart = props.getProperty("path.interiorPart");
        sdkConfig.path4ExteriorPart = props.getProperty("path.exteriorPart");
        sdkConfig.path4Material = props.getProperty("path.material");
        sdkConfig.path4VehicleModelLine = props.getProperty("path.vehicleModelLine");
        sdkConfig.path4VehicleModel = props.getProperty("path.vehicleModel");

        sdkConfig.checkIntegrity();
        return sdkConfig;
    }


    private void checkIntegrity() {
        // TODO
    }

    private String mobyHostCarCenter;
    private String mobyHostProductCenter;

    private String path4VehicleInfo;
    private String path4InteriorPart;
    private String path4ExteriorPart;
    private String path4Material;
    private String path4VehicleModelLine;
    private String path4VehicleModel;

    // 单独设置地址
    public void setMobyHost(String mobyHostCarCenter, String mobyHostProductCenter) {
        this.mobyHostCarCenter = mobyHostCarCenter;
        this.mobyHostProductCenter = mobyHostProductCenter;
    }

    public String getUrlByApiType(MobyApiType apiType) {
        if (null == apiType) {
            throw new RuntimeException("apiType should not be null");
        }

        switch (apiType) {
            case Material:
                return mobyHostCarCenter + path4Material;
            case ExteriorPart:
                return mobyHostCarCenter + path4ExteriorPart;
            case InteriorPart:
                return mobyHostCarCenter + path4InteriorPart;
            case VehicleInfo:
                return mobyHostCarCenter + path4VehicleInfo;

            case VehicleModel:
                return mobyHostProductCenter + path4VehicleModel;
            case VehicleModelLine:
                return mobyHostProductCenter + path4VehicleModelLine;
            default:
                throw new RuntimeException("unknown apiType:" + apiType.name);
        }
    }

}
